package com.intel.two.service.community.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.bo.community.SecurityBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.community.SecurityMapper;
import com.intel.two.pojo.community.Security;
import com.intel.two.service.community.SecurityService;
import com.intel.two.vo.community.securityBarVo;
import com.intel.two.vo.community.securityPieVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("securityService")
public class SecurityServiceImpl implements SecurityService {
    @Autowired
    private SecurityMapper  securityMapper;
    @Override
    public LayuiPageVo<Security> securityPageData(Map<String, Object> params) {

        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Security> securities =  securityMapper.selectByParams(params);

        /*分页*/
        PageInfo<Security> securityPageInfo = new PageInfo<>(securities);
        LayuiPageVo<Security> securityLayuiPageVo = new LayuiPageVo<>();
        securityLayuiPageVo.setCode(0);
        securityLayuiPageVo.setMsg("学生分页列表数据");
        securityLayuiPageVo.setCount(securityPageInfo.getTotal());
        securityLayuiPageVo.setData(securityPageInfo.getList());
        return securityLayuiPageVo;
    }
    /*增加*/
    @Override
    public int addSecurity(Security security) {
        return securityMapper.addSecurity(security);
    }

    @Override
    public List<SecurityBo> securityExcelData(Security security) {
        List<Security> securities = securityMapper.securityExcelData(security);
        //把po 转为 bo
        List<SecurityBo> collect = securities.stream()
                .map((security1 -> {
                    SecurityBo securityBo = new SecurityBo();
                    BeanUtils.copyProperties(security1, securityBo);
                    return securityBo;
                })).collect(Collectors.toList());

        return collect;
    }

    /*删除*/
    @Override
    public int deleteSecurity(Integer security_id) {
        return securityMapper.deleteSecurity(security_id);
    }

    @Override
    public int deleteSecurityAll(Map<String, Object> map) {
        String str = (String) map.get("security_id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return securityMapper.deleteSecurityAll(list);
    }

    @Override
    public int updateSecurity(Security security) {
        return securityMapper.updateSecurity(security);
    }

    @Override
    public List<securityBarVo> sexBarData() {
        return securityMapper.sexBarData();
    }

    @Override
    public List<securityPieVo> sexPieData() {
        return securityMapper.sexPieData();
    }


}

